package com.heima.miniofile.config;

import com.heima.miniofile.properties.MinIoConfigProperties;
import com.heima.miniofile.service.Impl.MinIoFileStorageServiceImpl;
import com.heima.miniofile.service.MinIoFileStorageService;
import io.minio.MinioClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@EnableConfigurationProperties({MinIoConfigProperties.class})
// 当引入MinIoFileStorageService接口时
@ConditionalOnClass(MinIoFileStorageService.class)
public class MinIoConfig {

    @Autowired
    private MinIoConfigProperties minIOConfigProperties;

    @Bean
    public MinioClient buildMinioClient() {
        return MinioClient
                .builder()
                .credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey())
                .endpoint(minIOConfigProperties.getEndpoint())
                .build();
    }
    @Bean
    @ConditionalOnMissingBean // 如果用户自己定义过实现，则不覆盖
    public MinIoFileStorageService minIoFileStorageService() {
        return new MinIoFileStorageServiceImpl();
    }
}
